class InsMonitorRule {
    constructor({
                    id = '',
                    insId = '',
                    subtype = '',
                    valueStart = '',
                    valueEnd = '',
                    keywordRange = '',
                    keywordRangeList = [],
                } = {}) {
        this.id = id
        this.insId = insId
        this.subtype = subtype
        this.valueStart = valueStart
        this.valueEnd = valueEnd
        this.keywordRange = keywordRange
        this.keywordRangeList = keywordRangeList
    }
}

//vue
new BaseValidateVue({
    el: '#app',
    data: function () {
        return {
            opers: [],
            houses: [],
            options: [
                {
                value: 0,
                label: '正文标题及正文本身',
                },
                {
                value: 1,
                label: '附件文件题目',
                }, {
                value: 2,
                label: '附件正文',
                }
            ],
            formData: {
                id: '',
                reason: '',
                log: '0',
                report: '0',
                effectTime: '',
                expiredTime: '',
                idcId: '',
                houseId: '',
                owner: 'ISMS添加',
                visible: '1',
                operationType: '0',
                level: '100000000000',
                timeStamp: '',
                reportStatus: '0',
                rawPacket: 'ISMS添加',
                insSource: '2',
                insMonitorRules: [
                    new InsMonitorRule(),
                ],
            },
            houseIdList: [],
            nowTime: '',
            ruleList: [
                new ValidateObject({
                    ruleName: 'value_start_validate',
                    validate: this.valueStartValidate,
                    msg: '请正确输入域名',
                }),
            ],
        }
    },
    methods: {
        createValidateList() {
            return this.ruleList;
        },
        async initOpers() {
            this.opers = await baseOperCompanyApi.selectList();
        },
        async idcIdChange() {
            this.houses = await baseHouseInfoApi.selectListByParam({idcId: this.formData.idcId});
        },
        async initHouses() {
            this.houses = await baseHouseInfoApi.selectList();
        },
        addInsRule() {
            this.formData.insMonitorRules.push(new InsMonitorRule({insId: this.formData.id}));
        },
        async submitForm() {
            this.$validator.validateAll().then(async (res) => {
                if (res) {
                    const effectDate = new Date(this.formData.effectTime);
                    const expiredDate = new Date(this.formData.expiredTime);
                    if (rx.dateBetween(this.nowTime, effectDate).milliSecond() < 0) {
                        await rxPrompt.msg('生效时间必须大于当前时间')
                        return;
                    }
                    if (rx.dateBetween(effectDate, expiredDate).milliSecond() < 0) {
                        await rxPrompt.msg('过期时间必须大于生效时间');
                        return;
                    }
                    //校验ip是否同一网段
                    if (this.ipValidate()) {
                        return;
                    }
                    var result = await insMonitorApi.update(this.formData);
                    if (result.code == 200) {
                        await rxPrompt.msg('修改成功')
                        forward('/command-manage/admin_commandmonitor')
                    } else {
                        await rxPrompt.msg(`新增失败,${result.msg}`)
                        return;
                    }
                } else {
                    rxPrompt.msg("输入存在错误！");
                }
            })

        },
        changeKeywordRange(index) {
            var insMonitorRule = this.formData.insMonitorRules[index]
            insMonitorRule.keywordRange = insMonitorRule.keywordRangeList.join();
        },
        changeHouseId() {
            this.formData.houseId = this.houseIdList.join()
        },
        deleteInsRule(index) {
            this.formData.insMonitorRules.splice(index, 1);
        },
        async initData() {
            var formData = forward.getData()
            //机房名称回显
            this.houseIdList = formData.houseId.split(',').map((id) => parseInt(id));
            this.formData = formData;
            this.formData.insMonitorRules.filter((obj) => obj.keywordRange).forEach((obj, index, array) => {
                array[index].keywordRangeList = obj.keywordRange.split(',').map((id) => parseInt(id));
            })
        },
        subtypeChange(index) {
            this.formData.insMonitorRules[index].valueStart = '';
            this.formData.insMonitorRules[index].valueEnd = '';
        },
        async valueStartValidate(val, args) {
            if (val === '') {
                return true;
            }
            const reg = /^(?=^.{3,255}$)[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+$/;
            return reg.test(val);
        },
        ipValidate() {
            var flag = false;
            this.formData.insMonitorRules.forEach((val, index, arr) => {
                const endField = 'valueEnd' + index;
                const {valueEnd = '', valueStart = '', subtype = ''} = val;
                if (valueEnd == null || valueEnd === '' || (parseInt(subtype) !== 4 && parseInt(subtype) !== 4)) {
                    return;
                }
                if (valueEnd.substring(0, 11) !== valueStart.substring(0, 11)) {
                    console.log("不在同一网段");
                    this.errors.remove(endField);
                    this.errors.add({
                        field: endField,
                        msg: '起始值和结束值不在同一网段'
                    });
                    flag = true;
                }
            });
            return flag;
        }
    },
    watch: {},
    mounted: async function () {
        await this.initData();
        this.initOpers();
        this.initHouses();
        this.nowTime = new Date();

    },

});
